<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>layui</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <link
      rel="stylesheet"
      href="../../lib/layui-v2.5.5/css/layui.css"
      media="all"
    />
    <link rel="stylesheet" href="../../css/public.css" media="all" />
    <style>
      body {
        background-color: #ffffff;
      }
      /* #demo1{position: fixed;width: 38vh;} */

      .scroll-body {
        height: 115px !important;
      }
    </style>
  </head>
  <body>
    <div class="layui-form layuimini-form" style="margin-left: -57px">
      <div class="layui-form-item">
        <label class="layui-form-label">终端类型</label>
        <div class="layui-input-block">
          <div id="demo1" class="xm-select-demo"></div>
          <tip>请选择终端类型</tip>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label required">网关编号</label>
        <div class="layui-input-block">
          <input
            type="text"
            name="channelCode"
            lay-verify="required"
            lay-reqtext="网关编号不能为空"
            placeholder="请输入网关编号"
            value=""
            class="layui-input"
          />
          <!-- <tip>填写自己管理账号的名称。</tip> -->
        </div>
      </div>
      <!-- <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div> -->
      <div class="layui-form-item">
        <label class="layui-form-label required">终端编号</label>
        <div class="layui-input-block">
          <input
            type="number"
            onKeypress="return(/[\d\.]/.test(String.fromCharCode(event.keyCode)))"
            name="terminalCode"
            lay-verify="required"
            lay-reqtext="终端编号不能为空"
            placeholder="请输入终端编号"
            value=""
            class="layui-input"
          />
        </div>
      </div>
      <!-- <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="email" name="email" placeholder="请输入邮箱" value="" class="layui-input">
        </div>
    </div> -->
      <div class="layui-form-item">
        <label class="layui-form-label">终端名称</label>
        <div class="layui-input-block">
          <input
            type="text"
            autocomplete="off"
            name="terminalName"
            placeholder="请输入终端名称"
            lay-verify="required"
            lay-reqtext="终端名称不能为空"
            value=""
            class="layui-input"
          />
        </div>
      </div>
      <!-- <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div> -->
      <!-- <div class="layui-form-item">
        <label class="layui-form-label">终端类型</label>
        <div class="layui-input-block">
            <div id="demo1" class="xm-select-demo"></div>
             <tip>请选择终端类型</tip>
        </div>
    </div> -->
      <div class="layui-form-item">
        <div class="layui-input-block" style="line-height: 40px; color: red">
          <span id="Tip_terminal"></span>
          <button
            class="layui-btn"
            lay-submit
            lay-filter="saveBtn"
            style="float: right; border-radius: 10%"
          >
            确认
          </button>
          <!-- <button class="layui-btn" lay-submit lay-filter="saveBtn" style="float: right; border-radius: 10%;margin-top: 20px;">确认</button> -->
        </div>
      </div>
    </div>

    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../js/xm-select.js" charset="utf-8"></script>
    <script>
      layui.use(["form"], function () {
        var form = layui.form,
          layer = layui.layer,
          $ = layui.$;
        var token = sessionStorage.getItem("newtoken");
        var station = JSON.parse(sessionStorage.getItem("SaveStation"));
        var device_code = JSON.parse(sessionStorage.getItem("Savedevice"));

        /********xm-select************/
        var demo1 = xmSelect.render({
          el: "#demo1",
          radio: true,
          direction: "down",
          clickClose: true,
          on: function (data) {
            //arr:  当前多选已选中的数据
            var arr = data.arr[0];
            if (arr.name == "电缆测温NBIOT") {
              $('input[name="channelCode"]')
                .val("106.14.62.86")
                .attr("disabled", true);
            } else {
              $('input[name="channelCode"]').val("").attr("disabled", false);
            }
          },
          // toolbar: {show: true},
          data: [],
        });
        /*******终端类型type***********/
        $.ajax({
          url: "/oms-web/types/terminalTypes?token=" + token,
          dataType: "json",
          type: "get",
          contentType: "application/json",
          success: function (data) {
            if (data.code == -1) {
              window.location = "../login-1.html";
            }
            if (data.code === 0) {
              var terminal_arr = [];
              for (let i = 0; i < data.data.length; i++) {
                var terminal_list = {};
                terminal_list.name = data.data[i].text;
                terminal_list.value = data.data[i].typo;
                terminal_arr.push(terminal_list);
              }
              demo1.update({
                data: terminal_arr,
                autoRow: true,
              });

              // layer.close(index);
              // layer.msg("新增成功",{icon: 1,time:1000},function(){
              //     window.location.reload();
              // })
            } else {
              // layer.close(index);
              layer.msg(data.msg, { icon: 1, time: 1000 });
            }
          },
          error: function () {
            layer.msg("数据请求错误！");
            layer.closeAll();
          },
        });

        //监听提交
        form.on("submit(saveBtn)", function (data) {
          var str = data.field;
          if (str.select == "") {
            layer.msg("请选择终端类型！");
          } else {
            layer.confirm(
              "确定增加该终端吗？",
              {
                btn: ["确定", "取消"], //按钮
              },
              function () {
                // loading
                index = layer.load(
                  1,
                  {
                    shade: [0.5],
                    content: "请稍后...",
                    success: function (layerContentStyle) {
                      layerContentStyle.find(".layui-layer-content").css({
                        "padding-top": "35px",
                        "text-align": "left",
                        width: "120px",
                      });
                    },
                  },
                  { time: 6 * 1000 }
                );

                $.ajax({
                  url:
                    "/oms-web/candt/bindingPlus/?token=" +
                    token +
                    "&channelCode=" +
                    str.channelCode +
                    "&terminalCode=" +
                    str.terminalCode +
                    "&terminalName=" +
                    str.terminalName +
                    "&stationCode=" +
                    station.stationCode +
                    "&deviceCode=" +
                    device_code.deviceCode +
                    "&pointType=" +
                    str.select,
                  dataType: "json",
                  type: "post",
                  contentType: "application/json",
                  success: function (data) {
                    if (data.code == -1) {
                      window.location = "../login-1.html";
                    }
                    if (data.code === 0) {
                      layer.close(index);
                      layer.closeAll();
                      $("#Tip_terminal").text("");
                      layer.msg(
                        "新增终端成功！",
                        { icon: 1, time: 1000 },
                        function () {
                          parent.layer.close(iframeIndex);
                          window.parent.location.reload(); //修改成功后刷新父界面
                        }
                      );
                    } else {
                      layer.closeAll();
                      $("#Tip_terminal").text(
                        "该网关添加失败,请检查并重新添加！"
                      );
                      layer.msg(data.msg, { icon: 2, time: 2000 });
                    }
                  },
                  error: function () {
                    layer.msg("数据请求错误！");
                    layer.closeAll();
                  },
                });
              }
            );
          }

          // var index = layer.alert(JSON.stringify(data.field), {
          //     title: '最终的提交信息'
          // }, function () {

          //     // 关闭弹出层
          //     // layer.close(index);

          var iframeIndex = parent.layer.getFrameIndex(window.name);

          // });

          return false;
        });
      });
    </script>
  </body>
</html>
